<?php
class Malbum extends CI_Model
{
	function __construct()
	{
		parent::__construct();
		$this->tablename = 'yun_album';
	}
	
	function pagination($tid = 0, $pindex = 0, &$totalcount = 0, $perpage = 10) {
		$item = new stdClass();
		$startnumber = $pindex * $perpage;
		$this->db->limit($perpage, $startnumber);
		if($tid){
			$query = $this->db->query('select * from '.$this->tablename.' where category = '.$tid.' order by created limit '.$startnumber.','.$perpage);
		}else{
			$query = $this->db->query('select * from '.$this->tablename.' order by created limit '.$startnumber.','.$perpage);
		}
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}

		if($tid){
			$query1 = $this->db->query('select * from '.$this->tablename.' where category = '.$tid);
		}else{
			$query1 = $this->db->query('select * from '.$this->tablename);
		}
		$totalcount = $query1->num_rows() ;
		$query->free_result();
		$query1->free_result();
		return $item;
	}

	function get_all($tid=0 ,$order='created') {
		$item = new stdClass();
		if($tid){
			$query = $this->db->query('select * from '.$this->tablename.' where category = '.$tid.' order by '.$order.' limit 36 ');
		}else{
			$query = $this->db->query('select * from '.$this->tablename.' order by '.$order.' limit 36 ');
		}
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}
		$query->free_result();
		return $item;
	}
	
	function get_hot_all($tid=0) {
		$item = new stdClass();
		if($tid){
			$query = $this->db->query('select * from '.$this->tablename.' where category = '.$tid.' order by view_count limit 30 ');
		}else{
			$query = $this->db->query('select * from '.$this->tablename.' order by view_count limit 30 ');
		}
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}
		$query->free_result();
		return $item;
	}

	function get_one($aid) {
		$item = new stdClass();
		$query = $this->db->query('select * from '.$this->tablename.' where aid = '.$aid);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

	function get_user($uid) {
		$item = new stdClass();
		$query = $this->db->query('select * from yun_user where uid = '.$uid);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

	function get_last($aid) {
		$rid = $rid - 1 ;
		$item = new stdClass();
		$query = $this->db->query('select rid,title from '.$this->tablename.' where aid = '.$aid);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

	function get_next($aid) {
		$rid = $rid + 1 ;
		$item = new stdClass();
		$query = $this->db->query('select rid,title from '.$this->tablename.' where aid = '.$aid);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

    function exist_id($field,$id){
		$item = 0 ;
		$query = $this->db->query('select aid from '.$this->tablename.' where '.$field.' = '.$id);
		if ($query->num_rows() > 0) {
			$item = 1;
		}
		return $item;
	}

    function insert($arr){
		$id = $arr['album_id'] ;
		$field = 'album_id' ;
		if(!$this->exist_id($field ,$id)){                                    // 防止重复记录
			$this->db->insert($this->tablename,$arr);
			return  $this->db->insert_id(); 
		}
    }

    function update($id,$arr)
    {
        $this->db->where('aid',$id);
        $this->db->update($this->tablename,$arr);
    }

    function delete($id){
        $this->db->where('aid',$id);
        $this->db->delete($this->tablename);
    }

}